<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\SystemStore;
use App\Models\SystemStoreStaff;
use App\Models\User;
use App\Models\WechatUser;
use App\Traits\Controllers\AdminTraits;
use App\Traits\Controllers\UpdateStatusTraits;
use App\Transformers\System\SystemStoreStaffInfoTransformer;
use App\Transformers\System\SystemStoreStaffTransformer;
use App\Transformers\Wechat\WechatUserTransFormer;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use PhpParser\Builder;

class SystemStoreStaffController extends Controller
{
    use AdminTraits;
    use UpdateStatusTraits;

    public $model = SystemStoreStaff::class;

    public $transformer = SystemStoreStaffTransformer::class;

    public function index()
    {
        $info = $this->getQuery()
            ->pimp()
            ->with(['user'=>function($query){
                $query->select('id')
                    ->with('wechatUsers:user_id,avatar,nickname');
            }])->with('store:id,name')
            ->paginate($this->getPageLimit());
        return $this->response->paginator($info, new SystemStoreStaffTransformer());
    }

    /**
     * 返回微信用户信息
     */
    public function webchat()
    {

        $info = WechatUser::query()
            ->pimp()
            ->paginate($this->getPageLimit());
        return $this->response->paginator($info, new WechatUserTransFormer());
    }

    /**
     * 返回所有的门店
     */
    public function stores()
    {
        $info = SystemStore::query()
            ->pimp()
            ->select('name')
            ->get()
            ->toArray();
        return $this->response->array($info);

    }

    //修改
    public function StaffUpdate(SystemStoreStaff $systemStoreStaff, Request $request)
    {

        $systemStoreStaff = $this->getQuery()
            ->where('id', $systemStoreStaff->id)
            ->first();
        $systemStoreStaff->update($request->all());
        $systemStoreStaff->save();
        return $this->response->noContent();
    }

    //核销订单头部
    public function listHeader()
    {

        $user = $this->getQuery()
            ->pluck('user_id');
        //订单总和
        $order_count = Order::query()
            ->pimp()
            ->whereIn('user_id', $user)
            ->where('paid', 1)
            ->count();
        //订单金额总和
        $order_amount = Order::query()
            ->pimp()
            ->whereIn('user_id', $user)
            ->where('paid', 1)
            ->sum('pay_price');

        //当日开始时间
        $startTime = Carbon::now()->startOfDay();
        //当日结束时间
        $endTime = Carbon::now()->endOfDay();
        //当日订单数量
        $day_order = Order::query()
            ->pimp()
            ->whereIn('user_id', $user)
            ->where('paid', 1)
            ->whereBetween('pay_time', [$startTime, $endTime])
            ->count();
        //当日订单金额
        $day_amount = Order::query()
            ->pimp()
            ->whereIn('user_id', $user)
            ->where('paid', 1)
            ->whereBetween('pay_time', [$startTime, $endTime])
            ->sum('pay_price');

        $data = [
            [
                'name' => '订单数量',
                'order_count' => $order_count,
            ],
            [
                'name' => '订单总金额',
                'order_amount' => $order_amount,
            ],
            [
                'name' => '日订单数量',
                'day_order' => $day_order,
            ],
            [
                'name' => '日订单金额',
                'day_amount' => $day_amount
            ]

        ];
        return $this->response->array($data);
    }

    /**
     *核销的订单列表
     */
    public function listInfo(Request $request)
    {
        //处理传过来的搜索数据
        $this->formatIndexQuery($request);

        $info = $this->getQuery()
            ->pimp()
            ->with(['order' => function ($query) {
                $query->with('user.parent:id,nickname,avatar')
                ->with('orderCartInfo');

            }])->with('store')
            ->paginate($this->getPageLimit());
        return $this->response->paginator($info,new SystemStoreStaffInfoTransformer());
    }



    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
            case 'PUT':
                return [
                    'user_id' => 'required|exists:wechat_users',
                    'store_id' => 'required',
                    'avatar' => 'required',
                    'phone' => 'required|regex:/^1[3-9]\d{9}$/',
                    'staff_name' => 'required'
                ];
                break;
            case 'PATCH':
                return [
                    'status' => 'required'
                ];
                break;

        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
            'user_id.required' => '用户不能为空',
            'user_id.exists' => '用户不存在',
            'store_id.required' => '门店不能为空',
            'phone.required' => '手机号不能为空',
            'phone.regex' => '手机号码格式不正确',
            'avatar.required' => '头像不能为空',
            'staff_name.required' => '店员名称不能为空'

        ];
    }


}
